﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;
using WuDada.Core.Member.Domain;
using WuDada.Core.Generic.Util;

public partial class join : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private MemberFactory m_MemberFactory;
    private IMemberService m_MemberService;
    private HttpHelper m_HttpHelper;
    private WebMailService m_WebMailService;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_MemberFactory = new MemberFactory();
        m_MemberService = m_MemberFactory.GetMemberService();
        m_HttpHelper = new HttpHelper();
        m_WebMailService = new WebMailService();

        if (!IsPostBack)
        {
            ClearUI();
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        MemberVO memberVO = new MemberVO();
        UIHelper.FillVO(UpdatePanel1, memberVO);
        if (m_MemberService.GetMemberByLoginId(memberVO.Email) != null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("此Email已經加入過了，請輸入其他Email"), false);
        }
        else
        {
            memberVO.LoginId = txtEmail.Text.Trim();
            memberVO.Password = PasswordUtil.GetRandomPass(5, 8);
            memberVO.Token = Guid.NewGuid().ToString();
            memberVO.CreatedDate = DateTime.Now;
            memberVO.UpdatedDate = DateTime.Now;
            memberVO.CreateIP = m_HttpHelper.GetUserIp(Context);
            memberVO = m_MemberService.CreateMember(memberVO);

            //發信
            m_WebMailService.SendConfirmMail_ToMember(memberVO);

            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("加入成功，感謝您的加入，系統已發送認證信至您的信箱，請收信並完成會員認證步驟。"), false);
            ClearUI();
        }
    }

    private void ClearUI()
    {
        UIHelper.ClearUI(UpdatePanel1);
    }
}